package csu.web.mypetstore.persistence.impl;

import csu.web.mypetstore.domain.Account;
import csu.web.mypetstore.persistence.LogDao;
import csu.web.mypetstore.domain.Log;
import csu.web.mypetstore.persistence.DBUtil;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.*;

public class LogDaoImpl implements LogDao {
    private static final String GET_LOG = "SELECT " +
            "TIME1,LOG1," +
            "TIME2,LOG2," +
            "TIME3,LOG3," +
            "TIME4,LOG4," +
            "TIME5,LOG5," +
            "TIME6,LOG6," +
            "TIME7,LOG7," +
            "TIME8,LOG8," +
            "TIME9,LOG9," +
            "TIME10,LOG10 " +
            "FROM LOG WHERE USERID =?";
    private static final String UPDATE_LOG = "UPDATE LOG SET " +
            "TIME1 =?,LOG1 =?," +
            "TIME2 =?,LOG2 =?," +
            "TIME3 =?,LOG3 =?," +
            "TIME4 =?,LOG4 =?," +
            "TIME5 =?,LOG5 =?," +
            "TIME6 =?,LOG6 =?," +
            "TIME7 =?,LOG7 =?," +
            "TIME8 =?,LOG8 =?," +
            "TIME9 =?,LOG9 =?," +
            "TIME10 =?,LOG10 =? " +
            "WHERE USERID =?";

    @Override
    public void updateLog(List<Log> logList, Account account) {
        try {
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(UPDATE_LOG);
            for (int i=0;i<10;i++){
                if(logList.size()>i){
                    preparedStatement.setString(2*i+1,logList.get(i).getDate());
                    preparedStatement.setString(2*i+2,logList.get(i).getMessage());
                }else{
                    preparedStatement.setString(2*i+1,"null");
                    preparedStatement.setString(2*i+2,"null");
                }
            }
            preparedStatement.setString(21, account.getUsername());
            preparedStatement.executeUpdate();
            DBUtil.closePreparedStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public List<Log> getLog(Account account) {
        List<Log> logList = null;
        try {
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(GET_LOG);
            preparedStatement.setString(1, account.getUsername());
            ResultSet resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                logList=new ArrayList<>();
                for (int i = 0; i < 10; i++) {
                    if (!Objects.equals(resultSet.getString(2 * i + 1), "") && !Objects.equals(resultSet.getString(2 * i + 1), "null")) {
                        Log log=new Log(resultSet.getString(2 * i + 1),resultSet.getString(2 * i + 2));
                        logList.add(log);
                    }else{
                        break;
                    }
                }
            }
            DBUtil.closeResultSet(resultSet);
            DBUtil.closePreparedStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return logList;
    }
}
